<template>
  <div class="background">
    <a-card style="margin-bottom: 10px">
      <a-form :form="form" layout="inline" @submit="handleSubmit">
        <a-form-item label="上货季">
          <a-select style="width: 200px" placeholder="请选择上货季" v-model="formContent.scseason"
            @focus="getDictChildList('SEASON', 'seasonList')" allow-clear>
            <a-select-option :value="item.code" v-for="(item, index) in seasonList" :key="index">
              {{ item.name }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="产品大类">
          <a-select style="width: 200px" placeholder="请选择产品大类" v-model="formContent.scdl"
            @focus="getDictChildList('PRODUCT_BIGTYPE', 'scdlList')" allow-clear>
            <a-select-option :value="item.code" v-for="(item, index) in scdlList" :key="index">
              {{ item.name }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="产品编号">
          <a-input style="width:200px" allow-clear placeholder="请输入产品编号" v-model="formContent.sccode"></a-input>
        </a-form-item>
        <a-form-item label="产品名称">
          <a-input style="width:200px" allow-clear placeholder="请输入产品名称" v-model="formContent.scname"></a-input>
        </a-form-item>
        <a-form-item label="审核状态">
          <a-select style="width: 200px" placeholder="请选择审核状态" v-model="formContent.orderstatus" allow-clear>
            <a-select-option :value="'20'" :key="'20'">
              已审核
            </a-select-option>
            <a-select-option :value="'10'" :key="'10'">
              未审核
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item>
          <a-button type="primary" html-type="submit">搜索</a-button>
          <a-button type="primary" style="margin-left: 10px" @click="add">新建</a-button>
        </a-form-item>
      </a-form>

    </a-card>
    <a-card>
      <a-table bordered :loading="loading" :scroll="{ x: 1500 }" :pagination="pagination" :columns="columns"
        :data-source="dataSource" @change="handleTableChange">
        <template slot="status" slot-scope="txt, record">
          <span v-for="item, i in statusList" :key="i">
            <span v-if="item.value == record.status">
              {{ item.name }}
            </span></span>
        </template>
        <template slot="ordertype" slot-scope="txt, record">
          <span v-if="record.ordertype == 1">调样</span>
          <span v-if="record.ordertype == 2">扩色</span>
          <span v-if="record.ordertype == 3">开发部调样</span>
          <span v-if="record.ordertype == 4">生产部调样</span>
          <span v-if="record.ordertype == 5">套色</span>
        </template>
        <template slot="flag" slot-scope="txt, record">
          <span v-if="record.flag == 'Y'">已审核</span>
          <span v-if="record.flag == 'N'">草稿</span>
        </template>

        <template slot="action" slot-scope="txt, record">
          <a class="mr-10" v-if="record.flag == 'N' && billtype == 10" @click="add(record)">
            修改
          </a>
          <a class="mr-10" v-if="billtype == 20" @click="add(record)">
            审核
          </a>
          <a-popconfirm  v-if="record.flag == 'N'" :title="`确定要删除该项吗?`" @confirm="submit(record)">
            <a
              class="mr-10"
            >
              提交
            </a>
          </a-popconfirm>
          <a-popconfirm
             v-if="billtype == 20"
              :title="`确定要驳回该项吗?`"
              @confirm="reject(record)"
            >
            <a
              class="mr-10"
            >
              驳回
            </a>
            </a-popconfirm>
        </template>
      </a-table>
    </a-card>

    <create v-if="showAdd" :accRetrievalSampleDetailId="accRetrievalSampleDetailId" @close="close"></create>
  </div>
</template>
    
<script>
import { orderReject, getDictChildList, orderSubmit, operation, getOrderList } from '@api/material/index'
import { postAction } from '@/api/manage'
import { reject } from 'lodash'

const columns = [

  {
    title: '单据编号',
    width: 170,
    dataIndex: 'billno',
    fixed: 'left'
  },
  {
    title: '上货季',
    width: 100,
    dataIndex: 'scseasonValue'
  },
  {
    title: '订单类型',
    width: 120,
    dataIndex: 'ordertypevalue',
    // scopedSlots: { customRender: 'ordertypevalue' }
  },
  {
    title: '产品编号',
    width: 130,
    dataIndex: 'sccode',
    scopedSlots: { customRender: 'sccode' }
  },
  {
    title: '产品名称',
    width: 250,
    dataIndex: 'scname',
  },
  {
    title: '产品成分',
    width: 230,
    dataIndex: 'sccf',
  },
  {
    title: '面料克重',
    width: 100,
    dataIndex: 'scmlkz',
  },
  {
    title: '有效门幅',
    width: 180,
    dataIndex: 'scmf',
  },
  {
    title: '订货总数量',
    width: 110,
    dataIndex: 'ordersl',
  },
  {
    title: '下单备注',
    width: 200,
    dataIndex: 'memo1'
  },
  {
    title: '供应商',
    width: 130,
    dataIndex: 'scsupvalue'
  },
  {
    title: '制单人员',
    width: 170,
    dataIndex: 'inputor'
  },
  {
    title: '下单日期',
    width: 220,
    dataIndex: 'date1'
  },
  {
    title: '状态',
    width: 90,
    dataIndex: 'flag',
    scopedSlots: { customRender: 'flag' }

  },
  {
    title: '操作',
    width: 170,
    fixed: 'right',
    scopedSlots: { customRender: 'action' }
  }
]
export default {
  components: {
  },
  data() {
    return {
      urlKey: 'basemgt-material/',
      columns,
      statusList: [{ value: 10, name: '草稿' }, { value: 50, name: '退回草稿' }, { value: 70, name: '强制作废' }],
      form: this.$form.createForm(this),
      showAdd: false,
      loading: false,
      dataSource: [],
      seasonList: [],
      scdlList: [],
      billtype:10,
      pagination: {
        total: 0,
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '20', '30', '40'],
        showQuickJumper: true,
        showSizeChanger: true,
        showTotal: total => `共 ${total} 条记录`,
        itemRender: (current, type, originalElement) => {
          if (type === 'prev') {
            return <a>上一页</a>
          } else if (type === 'next') {
            return <a>下一页</a>
          }
          return originalElement
        }
      },
      formContent: {
        scseason: undefined,
        scdl: undefined,
        scname: '',
        sccode: '',
        djlb: 1,
        orderstatus: undefined,
      },
    }
  },
  computed: {
    jobNumber() {
      return this.$store.getters.userInfo.workNo || ''
    }
  },
  mounted() {
    this.loadData()
  },
  created() {
    let pathname = window.location.href
    let pathArr = pathname.split('/')
    if (pathArr.includes('exam')) {
      this.billtype = 20
    }
  },

  methods: {
    close() {
      this.showAdd = false
      this.loadData()
    },
    add(record) {
      if (record) {
        this.$router.push({ path: '/material-file/retrievalsample/add', query: { billno: record.billno } })
      } else {
        this.$router.push({ path: '/material-file/retrievalsample/add' })
      }
    },
    handleTableChange(pagination) {
      this.pagination = pagination
      this.loadData()
    },
    handleSubmit(e) {
      e.preventDefault()
      this.getList(1)
    },
    loadData() {
      this.getList()
    },
    async getDictChildList(name, list) {
      const params = {
        name: name
      }
      try {
        const res = await getDictChildList(params)
        if (res.code == 200) {
          this[list] = res.data
        } else {
          if (res.msg) {
            this.$message.error(res.msg)
          } else {
            this.$message.error('获取数据字典失败！')
          }
        }
      } catch (error) {
        this.$message.error('获取数据字典失败！')
      }
    },

    async getList(key) {
      this.loading = true
      const params = this.formContent
      params.current = this.pagination.current
      params.size = this.pagination.pageSize
      params.billtype = this.billtype
      if (key) {
        params.current = 1
      }
      try {
        const res = await getOrderList(params)
        if (res.code == 200) {
          this.dataSource = res.data.records
          this.pagination.total = res.data.total
        } else {
          this.$message.warning(res.msg)
        }
      } catch (error) {
        this.$message.warning('获取列表失败！')
      } finally {
        this.loading = false
      }
    },
    async reject(record) {
      const res = await orderReject({ billno: record.billno, jobNumber: this.jobNumber, sccode: record.sccode,})
      if (res.code == 200) {
        this.$message.success('驳回成功！')
        this.loadData()
      } else {
        this.$message.warning(res.msg)
      }
    },
    async submit(record) {
      const res = await orderSubmit({ billno: record.billno, jobNumber: this.jobNumber, sccode: record.sccode,})
      if (res.code == 200) {
        this.$message.success('提交成功！')
        this.loadData()
      } else {
        this.$message.warning(res.msg)
      }
    },
    async doOperation(record, operate) {
      let params = {
        accId: record.accId,
        jobNumber: this.jobNumber,
        operate: operate
      }
      const res = await operation(params)
      if (res.code == 200) {
        this.$message.success('状态更新成功！')
        this.loadData()
      } else {
        this.$message.warning(res.msg)
      }
    }
  }
}
</script>
<style lang="less" scoped>
.background {
  background: transparent;
}

.mr-10 {
  margin-left: 10px;
}
</style>